(function($) {
	
	$.fn.isMessageDialogue = function(){
		
		return $(this).each(function(){
			
			var deleteButton = $('.message-item .destory',this),
			    replyTextarea = $('textarea[name="pmContent"]',this),
			    replyForm = $('#reply_form',this),
			    pMessage = $('#pmContent',this);
			
			$(deleteButton).click(function(){
				var $this = $(this);
				if(confirm(messages.deletePrivateMessage)){
					$.ajax({
						async:false,
						url:$(this).attr('href'),
						type:'GET',
						dataType:'json',
						beforeSend: function (xhr) {
							 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
						},
						success:function(json){
							if(json.success){
								$this.parents('.message-item').fadeOut(300);
							}
						}
					});
				}
				return false;
			}); 
			
			$(replyTextarea).maxlength({
				statusElement:$('.wordNum'),
				maxlength:300
			});
			
			$(replyForm).submit(function(){
				if($(pMessage).val() == ''){
					$(pMessage).css('background','#F5CFCF');
					$(pMessage).animate({
					    opacity: 0.3
					}, 300, function() {
						$(pMessage).css('background','#fff').css('opacity', '1');
					});
					return false;
				}
			});
		});
	};
})(jQuery);